package tech.tuister.cninfo.download.api;

import com.alibaba.fastjson2.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import tech.tuister.cninfo.download.util.CninfoAPI;
import tech.tuister.cninfo.download.util.HttpUtils;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import static javax.swing.UIManager.get;

@Slf4j
@CninfoAPI
public class InformationTopSearchAPI {

    private static final String URL = "http://www.cninfo.com.cn/new/information/topSearch/query";

    //http://www.cninfo.com.cn/new/information/topSearch/query?keyWord=中信特钢&maxNum=10
    // http://www.cninfo.com.cn/new/information/topSearch/query?keyWord=中信特钢&maxNum=10
    public static List<Result> query(Query query) throws IOException {
        String json = HttpUtils.post(URL, HttpUtils.buildParams(query));
        log.debug("response: {}", json);
        return JSON.parseArray(json, Result.class);
    }

    public static Result query(String keyword) throws IOException {
        List<Result> results = query(new Query(keyword));
        if (results != null && !results.isEmpty()) {
            return results.get(0);
        }
        return null;
    }

    @Data
    static class Query implements Serializable {
        private static final long serialVersionUID = 5048725816657585362L;
        private String keyWord;
        private int maxNum = 10;

        public Query(String keyWord) {
            this.keyWord = keyWord;
        }

        public Query(String keyWord, int maxNum) {
            this.keyWord = keyWord;
            this.maxNum = maxNum;
        }
    }

    // [{"code":"000708","pinyin":"zxtg","category":"A股","type":"shj","orgId":"gssz0000708","zwjc":"中信特钢"}]
    @Data
    static class Result implements Serializable {
        private static final long serialVersionUID = 3842146478948979522L;

        private String code;
        private String pinyin;
        private String category;

        private String type;

        private String orgId;

        private String zwjc;


    }
}
